package logic

import (
	"context"
	"github.com/pkg/errors"
	"zg2_zy/zy6_company/companyRpc/models"

	"zg2_zy/zy6_company/companyRpc/company"
	"zg2_zy/zy6_company/companyRpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddJobLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddJobLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddJobLogic {
	return &AddJobLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddJobLogic) AddJob(in *company.AddJobRequest) (*company.AddJobResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var c models.Company
	if err = c.GetJobById(); err != nil {
		return nil, errors.New("职位信息获取失败")
	}

	if in.Job == "" || in.Types == "" || in.Ads == "" ||
		in.Salary == 0 || in.Exp == "" || in.Degree == "" {
		return nil, errors.New("<职位信息不能为空>")
	}

	if err = c.Create(); err != nil {
		return nil, errors.New("<职位发布失败>")
	}

	c.Job = in.Job
	c.Ads = in.Ads
	c.Types = in.Types
	c.Salary = float64(in.Salary)
	c.Exp = in.Exp
	c.Degree = in.Degree

	return &company.AddJobResponse{
		Id: int64(c.ID),
	}, nil
}
